﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.HeatingControlProject._2.数据层;
using thinger.HeatingControlProject._4.实体类;


namespace thinger.HeatingControlProject._1.界面层
{
    public partial class FrmUserManage : Form
    {
        #region 无边框拖动 

        private Point mPoint;
        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }

        #endregion

        private SysAdminService adminService = new SysAdminService();
        List<SysAdmin> listAdmin = null;
        public FrmUserManage()
        {
            InitializeComponent();
            //禁止生成列
            this.dgv_UserList.AutoGenerateColumns = false;
            //显示所有用户列表
            this.listAdmin = adminService.QuerySysAdmins();
            if(listAdmin != null)
            {
                this.dgv_UserList.DataSource = listAdmin;

                SysAdmin admin = this.listAdmin.First();
                ShowCurrentUserInfo(admin);
            }

        }

        //显示行号
        private void dgv_UserList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPostPaint(this.dgv_UserList, e);
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //单击单元格事件显示当前用户信息
        private void dgv_UserList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex >= 0)
            {
                SysAdmin admin = this.listAdmin[e.RowIndex];
                ShowCurrentUserInfo(admin);
            }
        }

        //显示当前用户信息
        private void ShowCurrentUserInfo(SysAdmin admin)
        {
            this.txt_LoginName.Text = admin.LoginName;
            this.txt_LoginPwd.Text = admin.LoginPwd;
            this.cmb_RoleName.Text = admin.RoleName;
        }
        //添加用户
        private void btn_Add_Click(object sender, EventArgs e)
        {
            //[1]输入内容非空判断
            if(this.txt_LoginName.Text.Length == 0)
            {
                MessageBox.Show("用户名不能为空！", "提示信息");
                return;
            }
            if(this.txt_LoginPwd.Text.Length == 0)
            {
                MessageBox.Show("用户密码不能为空！", "提示信息");
                return;
            }
            //[2]判断用户是否存在
            foreach(SysAdmin admin in listAdmin)
            {
                if(admin.LoginName == this.txt_LoginName.Text.Trim())
                {
                    MessageBox.Show("该用户已经存在！", "提示信息");
                    return ;
                }
            }
            //[3]封装用户对象
            SysAdmin sysAdmin = new SysAdmin()
            {
                LoginName = this.txt_LoginName.Text.Trim(),
                LoginPwd = this.txt_LoginPwd.Text.Trim(),
                RoleName = this.cmb_RoleName.Text.Trim()
            };
            //[4]添加用户到数据库
            try
            {
                adminService.AddSysAdmin(sysAdmin);
                //添加到当前用户集合中,并更新显示
                listAdmin.Add(sysAdmin);
                this.dgv_UserList.DataSource = null;
                this.dgv_UserList.DataSource = listAdmin;
            }
            catch (Exception ex)
            {

                MessageBox.Show("添加用户失败！" + ex.Message, "信息提示");
            }

        }

        private void btn_Modify_Click(object sender, EventArgs e)
        {
            //[1]输入内容非空判断
            if (this.txt_LoginName.Text.Length == 0)
            {
                MessageBox.Show("用户名不能为空！", "提示信息");
                return;
            }
            if (this.txt_LoginPwd.Text.Length == 0)
            {
                MessageBox.Show("用户密码不能为空！", "提示信息");
                return;
            }
            //[2]判断用户是否存在,不能和其他用户重名
            int currentLoginId = Convert.ToInt32(this.dgv_UserList.CurrentRow.Cells["LoginId"].Value);//当前选中行用户ID

            foreach (SysAdmin admin in listAdmin)
            {
                if(admin.LoginName == this.txt_LoginName.Text.Trim() && admin.LoginId != currentLoginId)
                {
                    MessageBox.Show("当前修改的用户名和其他用户名重复！","提示信息");
                    return;
                }
            }
            //[3]封装用户对象
            SysAdmin sysAdmin = new SysAdmin()
            {
                LoginName = this.txt_LoginName.Text.Trim(),
                LoginPwd = this.txt_LoginPwd.Text.Trim(),
                RoleName = this.cmb_RoleName.Text.Trim(),
                LoginId = currentLoginId
            };
            //[4]提交修改用户
            try
            {
                adminService.ModifySysAdmin(sysAdmin);
                //同步更新用户信息
                this.listAdmin = adminService.QuerySysAdmins();
                this.dgv_UserList.DataSource = null;
                this.dgv_UserList.DataSource = listAdmin;
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改用户信息失败！"+ex.Message,"提示信息");
            }
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            int currentLoginId = Convert.ToInt32(this.dgv_UserList.CurrentRow.Cells["LoginId"].Value);//当前选中行用户ID
            DialogResult result = MessageBox.Show("确认删除当前用户吗？","系统询问",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
            if (result == DialogResult.Cancel)
            {
                return;
            }
            try
            {
                adminService.DeleteSysAdmin(currentLoginId);
                //同步更新用户信息
                this.listAdmin = adminService.QuerySysAdmins();
                this.dgv_UserList.DataSource = null;
                this.dgv_UserList.DataSource = listAdmin;
            }
            catch (Exception ex)
            {

                MessageBox.Show("删除用户信息失败！" + ex.Message, "提示信息");
            }
        }
    }
}
